/************************************************************************************************
 * test examples of 100 interesting program in C
 * test 079.c
 * Approximation of PI
 ***********************************************************************************************/

#include <stdio.h>
#include <stdlib.h>

#define N 300000

/*
 * use random method, to fill a square with random points
 * at the same time, accumulate points which is in the 1/4 circle inner the square
 * after enough points are filled, we assume that they are equaldistributed in the square,
 * and let it present area of figure
 * since the area of 1/4 circle is 1/4*PI (as area of the square is 1)
 * we get PI = accumulated points/all points*4
 */

int main()
{
    int x, y;
    int c = 0, d = 0;
    while (c++<=N)
    {
        x = (int)(100.0*rand()/(RAND_MAX+1.0));
        y = (int)(100.0*rand()/(RAND_MAX+1.0));
        if (x*x + y*y < 10000)
            d++;
    }
    printf("PI = %f\n", 4*(double)d/N);
}

